package jjn.round2;

import commons.TreeNode;

/**
 * @author Jiang Jining
 * @since 2023-05-03 19:07
 */
public class Offer26_SubStructure {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        return (A != null && B != null) && (isSub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }
    
    private boolean isSub(TreeNode first, TreeNode second) {
        if (second == null) {
            return true;
        }
        if (first == null || first.val != second.val) {
            return false;
        }
        return isSub(first.left, second.left) && isSub(first.right, second.right);
    }
}
